package cn.jbolt.config.common;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import cn.jbolt.util.Result;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * 处理result返回类的自定义序列化的方法
 */
@Configuration
public class ResultSerializer extends JsonSerializer<Result<?>> {
    @Override
    public void serialize(Result<?> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeStartObject();
        gen.writeNumberField("code", value.getCode());
        gen.writeObjectField("data", value.getData());
        gen.writeStringField("state", value.getState());
        gen.writeStringField("msg", value.getMsg());
        gen.writeEndObject();
    }
}